A$NoToilet = A$J.J4 == 4

A$Evict = A$J.J23_b > 0

A$J.J24_Eviction = mapvalues(A$J.J24_Eviction, from = c(888, 999), to = c(NA, NA))

A$Recog = mapvalues(A$J.J22, from = c(888, 999, -888), to = c(0,0,0)) #code don't knows as 0

Neigh = A %>% group_by(A.A7_Area.Neighborhood) %>% summarise(NeighDens = mean(NeighDens),
                                                             PropMost = mean( NeighPropMost),
                                                             Fractionalization = mean(NeighFractionalization),
                                                             PartyPropMost = sort(table(L.L72_Party.performance,useNA = 'no'),decreasing=T)[1] / n(),
                                                             PartyFrac = 1-sum((table(L.L72_Party.performance)/n())^2),
                                                             NeighVB = mean(L.L67_Effective.vote.bank),
                                                             AssetSum = mean(AssetSum),
                                                             Recog = mean(Recog),
                                                             Evict = mean(Evict, na.rm=T),
                                                             Secure = mean(J.J24_Eviction, na.rm=T),
                                                             NoToilet = mean(NoToilet),
                                                             MostNeed = names(sort(table(L.L19_Public.needs), decreasing = T))[1],
                                                             SecMostNeed = names(sort(table(L.L19_Public.needs), decreasing = T))[2],
                                                             PropMostNeed = sort(table(L.L19_Public.needs), decreasing = T)[1] / n(),
                                                             PropSecMostNeed = sort(table(L.L19_Public.needs), decreasing = T)[2] / n(),
                                                             City = City[1],
                                                             AvgYears = mean(YearsInCity, na.rm = T),    
                                                             PropUnder20 = mean(YearsInCity <= 20, na.rm = T),
                                                             PropUnder10 = mean(YearsInCity <= 10, na.rm = T),
                                                             PropBorn = mean(C.C14_Permanent.Residence.of.Jaipur.)
                                                             ) %>% data.frame()

Neigh$PropUnder20[which(is.na(Neigh$PropUnder20))] = 0 #NA's are places where everyone was born in city
Neigh$PropUnder10[which(is.na(Neigh$PropUnder10))] = 0 #NA's are places where everyone was born in city

#Create new individual variables from neighborhood variables
A$NeighVB = Neigh[match(A$A.A7_Area.Neighborhood, Neigh$A.A7_Area.Neighborhood),'NeighVB']
A$NeighLeaderFrac = Neigh[match(A$A.A7_Area.Neighborhood, Neigh$A.A7_Area.Neighborhood),'Fractionalization']
A$NeighPartyFrac = Neigh[match(A$A.A7_Area.Neighborhood, Neigh$A.A7_Area.Neighborhood),'PartyFrac']